﻿Public Class ViewParametrerGroupe



    Private Sub AdvancedDatagrid_AutoGeneratingColumn(sender As Object, e As Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
        If e.PropertyName.ToUpper().Trim() = "GROUPE" Or e.PropertyName.ToUpper().Trim() = "CODE_GARANTIE" Then
            e.Cancel = True
        End If
    End Sub

    Private Sub UpdateValues(datagrid As AdvancedDatagrid)
        Dim inputdata As New ViewModelInputBox
        PMTModule.UIService.ShowMessage(inputdata)

        If String.IsNullOrEmpty(inputdata.Value) Then Return
        If String.IsNullOrEmpty(inputdata.Value.Trim()) Then Return

        Dim value As Double

        If inputdata.Value.Trim().Contains("%") Then
            value = Convert.ToDouble(inputdata.Value.Trim().Replace("%", "").Trim()) / 100.0
        Else
            value = Convert.ToDouble(inputdata.Value.Trim())
        End If

        For Each elt In datagrid.SelectedCells
            Dim row As DataRowView = elt.Item

            row.Row(elt.Column.Header.ToString()) = value
        Next
    End Sub

    Private Sub CopyValues(datagrid As AdvancedDatagrid)
        Windows.Input.ApplicationCommands.Copy.Execute(Nothing, datagrid)
    End Sub
    Private Sub PasteValues(datagrid As AdvancedDatagrid)
        Dim str = Windows.Clipboard.GetData(Windows.DataFormats.Text).ToString()

        Dim dataview As DataView = datagrid.ItemsSource

        Dim table = ToDataTable(str, dataview.Table)

        If table Is Nothing Then Return

        Dim vm As ViewModelParametrerGroupe = DataContext

        If table.Columns.Contains("Generation") Then

            dataview.Sort = "Generation"

            For Each r As DataRow In table.Rows
                Dim rindex = dataview.Find(r("Generation"))

                If rindex >= 0 Then
                    For Each col As DataColumn In table.Columns
                        If col.ColumnName <> "Generation" Then
                            dataview(rindex)(col.ColumnName) = r(col.ColumnName)
                        End If
                    Next
                Else
                    Dim nrow = dataview.Table.NewRow()
                    nrow("Groupe") = vm.Groupe
                    nrow("Code_Garantie") = vm.Garantie
                    nrow("Generation") = r("Generation")

                    For Each col As DataColumn In table.Columns
                        If col.ColumnName <> "Generation" Then
                            nrow(col.ColumnName) = r(col.ColumnName)
                        End If
                    Next

                    dataview.Table.Rows.Add(nrow)
                End If
            Next
        End If
    End Sub

    Private Function ToDataTable(str As String, reftable As DataTable) As DataTable
        PMTModule.UIService.ShowMessage(str)

        Dim lines = str.Trim().Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)

        If lines.Count <= 1 Then Return Nothing

        Dim cols = lines(0).Trim().Split(New String() {vbTab}, StringSplitOptions.RemoveEmptyEntries)

        Dim result As New DataTable

        For Each c In cols

            If Not reftable.Columns.Contains(c) Then Return Nothing

            result.Columns.Add(c, reftable.Columns(c).DataType)
        Next

        For i = 1 To lines.Length - 1
            Dim values = lines(i).Split(New String() {vbTab}, StringSplitOptions.None)

            Dim qvalues = From v In values
                          Select CType(If(String.IsNullOrEmpty(v), Nothing, v), Object)

            result.Rows.Add(qvalues.ToArray())
        Next

        Return result
    End Function

    Private Sub Modifier_Valeur_MenuItem_ComFrais_Click(sender As Object, e As Windows.RoutedEventArgs)
        UpdateValues(DgridComFrais)
    End Sub
    Private Sub Modifier_Valeur_MenuItem_Reass_Click(sender As Object, e As Windows.RoutedEventArgs)
        UpdateValues(DgridReass)
    End Sub
    Private Sub Modifier_Valeur_MenuItem_Deroule_Click(sender As Object, e As Windows.RoutedEventArgs)
        UpdateValues(DgridDeroule)
    End Sub
    Private Sub Modifier_Valeur_MenuItem_NvleGen_Click(sender As Object, e As Windows.RoutedEventArgs)
        UpdateValues(DgridNvleGen)
    End Sub

    Private Sub Copier_MenuItem_ComFrais_Click(sender As Object, e As Windows.RoutedEventArgs)
        CopyValues(DgridComFrais)
    End Sub
    Private Sub Coller_MenuItem_ComFrais_Click(sender As Object, e As Windows.RoutedEventArgs)
        PasteValues(DgridComFrais)
    End Sub

    Private Sub DgridComFrais_CopyingRowClipboardContent(sender As Object, e As Windows.Controls.DataGridRowClipboardEventArgs) Handles DgridComFrais.CopyingRowClipboardContent

        If e.IsColumnHeadersRow Then

            Dim op = 1

            Return
        End If

        Dim q = Aggregate elt In e.ClipboardRowContent Where elt.Column.Header.ToString().ToLower() = "generation" Into Count()

        If q = 0 Then
            Dim it As DataRowView = e.Item
            Dim col = From elt In DgridComFrais.Columns Where elt.Header.ToString().ToLower() = "generation" Select elt
            e.ClipboardRowContent.Add(New Windows.Controls.DataGridClipboardCellContent(e.Item, col.First(), it("Generation")))
        End If
    End Sub

    Private Sub Recalculer_MenuItem_ComFrais_Click(sender As Object, e As Windows.RoutedEventArgs)
        Dim manager As New DataManager
        manager.Recalculer_Table_Taux_Com()
    End Sub
End Class

